<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="uname"
      ></el-input>
      <el-button @click="remove">清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="editList(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delList(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params._limit"
        @current-change="pageChange"
      >
      </el-pagination>
      <el-button size="small" plain @click="addList">+ 新增</el-button>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      title="提示"
      :visible="dialogVisible"
      width="30%"
      @close="closeDialog"
    >
      <!-- 表单内容 -->
      <el-form ref="addForm" label-width="80px" :model="addTask" :rules="rules">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="addTask.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="addTask.des"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getListAPI,
  addListAPI,
  getDetailAPI,
  editListAPI,
  delListAPI,
  searchAPI,
} from "@/api/list.js";
import { v4 as uuidv4 } from "uuid";
export default {
  data() {
    return {
      list: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      total: 0, //总数,
      dialogVisible: false,
      // 新增任务 数据体
      addTask: {
        name: "",
        des: "",
        id: "",
      },
      // 校验规则
      rules: {
        name: [
          { required: true, message: "任务名称不能为空", trigger: "blur" },
        ],
        des: [{ required: true, message: "任务描述不能为空", trigger: "blur" }],
      },
      uname: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const res = await getListAPI(this.params);
      console.log(res);
      this.list = res.data;
      this.total = Number(res.headers["x-total-count"]);
      console.log(this.total);
    },
    // 点击下面页数 改变当前页
    pageChange(page) {
      console.log(page);
      this.params._page = page;
      this.getList();
    },
    // 新增功能
    addList() {
      this.dialogVisible = true;
    },
    // 新增 兜底校验
    confirm() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (!this.addTask.id) {
            const id = uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
            console.log(uuidv4());
            this.addTask.id = id;
            // 调用新增接口
            await addListAPI(this.addTask);
            // 提示
            this.$message.success("新增任务成功");
          } else {
            // 调用编辑接口
            await editListAPI(this.addTask);
            // 提示
            this.$message.success("修改任务成功");
          }

          // 渲染页面
          this.getList();

          // 关闭弹框
          this.dialogVisible = false;
          // 重置表单
          this.$refs.addForm.resetFields();
          // 清空表单数据
          this.addTask = {
            name: "",
            des: "",
            id: "",
          };
        }
      });
    },
    // 点x
    closeDialog() {
      this.dialogVisible = false;
      // 重置表单
      this.$refs.addForm.resetFields();
    },
    // 编辑功能
    async editList(id) {
      console.log(id);
      this.dialogVisible = true;
      // 数据回填
      const res = await getDetailAPI(id);
      // console.log(res);
      this.addTask = res.data;
    },
    // 删除功能
    async delList(id) {
      // console.log(id);
      await delListAPI(id);
      this.getList();
    },
    // 搜索功能
    async search() {
      if (this.uname === "") {
        this.getList();
      } else {
        // 调用搜索接口
        const res = await searchAPI(this.uname);
        console.log(res);
        // 将接口返回的数组赋值给list
        this.list = res.data;
        this.total = this.list.length;
      }
    },
    // 搜索 清除
    remove() {
      this.uname = "";
      this.getList();
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
